package com.neulogistics.contrib.usercenter.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.Role;
import com.neulogistics.contrib.usercenter.domain.repository.IRoleRepository;
import com.neulogistics.contrib.usercenter.infrastructure.persistence.converter.RoleEntityPOConverter;
import com.neulogistics.contrib.usercenter.infrastructure.persistence.orm.po.RolePO;
import com.neulogistics.contrib.usercenter.infrastructure.persistence.orm.service.IRolePOService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class RoleRepository implements IRoleRepository {
    @Resource
    IRolePOService rolePOService;

    @Override
    public void save(Role role) {
        RolePO rolePO = RoleEntityPOConverter.entityToPO(role);
        rolePOService.saveOrUpdate(rolePO);
    }

    @Override
    public Role findById(Integer id) {
        RolePO rolePO = rolePOService.getById(id);
        Role role = RoleEntityPOConverter.POToEntity(rolePO);
        return role;
    }

    @Override
    public void deleteById(Integer id) {
        rolePOService.removeById(id);
    }

    @Override
    public Role findByLevel(Integer roleLevel) {
        LambdaQueryWrapper<RolePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePO::getRoleLevel, roleLevel);
        RolePO rolePO = rolePOService.getOne(queryWrapper);
        Role role = RoleEntityPOConverter.POToEntity(rolePO);
        return role;
    }
}
